package servlet.admin;

import com.google.gson.Gson;
import entity.Comment;
import service.CommentService;
import service.Impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns = "/admin/CommentUpdate")
public class CommentEidtServlet extends HttpServlet {
    private CommentService commentService = new CommentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        Integer id = Integer.parseInt(req.getParameter("id"));
        String content = req.getParameter("content");
        Comment comment = new Comment();
        comment.setId(id);
        comment.setContent(content);
        boolean success = commentService.update(comment);
        Gson gson = new Gson();
        String jsonResponse = gson.toJson(new Response(success));
        resp.getWriter().write(jsonResponse);
    }
    // 简单的响应类
    private class Response {
        private boolean success;
        private String error;
        public Response(boolean success) {
            this.success = success;
        }
        public Response(boolean success, String error) {
            this.success = success;
            this.error = error;
        }
    }
}
